package com.jzo2o.market.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.*;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.mapper.CouponMapper;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.CouponOperationPageQueryReqDTO;
import com.jzo2o.market.model.dto.response.CouponInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponUseBackService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
@Slf4j
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Resource(name = "seizeCouponScript")
    private DefaultRedisScript<String> seizeCouponScript;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IActivityService activityService;

    @Resource
    private ICouponUseBackService couponUseBackService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;

    /**
     * @param dto
     * @return com.jzo2o.common.model.PageResult<com.jzo2o.market.model.dto.response.CouponInfoResDTO>
     * @description: 运营端优惠券领取记录分页查询
     **/
    @Override
    public PageResult<CouponInfoResDTO> pageQuery(CouponOperationPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () -> baseMapper.pageQuery(dto.getActivityId()));
    }

    /**
     * @param status 优惠券状态，1：未使用，2：已使用，3：已过期
     * @param lastId 上一次查询最后一张优惠券id
     * @return java.util.List<com.jzo2o.market.model.dto.response.CouponInfoResDTO>
     * @description: 用户端我的优惠券列表
     **/
    @Override
    public List<CouponInfoResDTO> myCouponList(Integer status, Long lastId) {
        List<Coupon> couponList = this.lambdaQuery()
                .eq(status != null, Coupon::getStatus, status)
                .eq(Coupon::getUserId, UserContext.currentUserId())
                .orderByDesc(Coupon::getCreateTime)
                .list();
        if (CollUtils.isEmpty(couponList)) {
            return CollUtils.emptyList();
        }

        return BeanUtils.copyToList(couponList, CouponInfoResDTO.class);
    }

    @Override
    public void processExpireCoupon() {
        lambdaUpdate()
                .set(Coupon::getStatus, CouponStatusEnum.INVALID.getStatus())
                .eq(Coupon::getStatus, CouponStatusEnum.NO_USE.getStatus())
                .le(Coupon::getValidityTime, DateUtils.now())
                .update();
    }
}
